package com.yuke.cloud.service.uac.controller.frontend;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.wmc.service.WmcStorageFeignApi;
import com.yuke.cloud.service.api.uac.dto.AccEmployeeInfoDto;
import com.yuke.cloud.service.uac.dto.UserGroupDto;
import com.yuke.cloud.service.uac.entity.AccCheckinLog;
import com.yuke.cloud.service.uac.entity.AccEmployeeGroup;
import com.yuke.cloud.service.uac.service.AccCheckinLogService;
import com.yuke.cloud.service.uac.service.AccEmployeeGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* Created by zn on 2019/11/26.
*/
@Api(value = "WEB - AccEmployeeGroupController", tags = "处理AccEmployeeGroup相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/acc/employee_group")
public class AccEmployeeGroupController extends BaseController {
    @Resource
    private AccEmployeeGroupService accEmployeeGroupService;
    @Resource
    private AccCheckinLogService accCheckinLogService;
    @Resource
    private WmcStorageFeignApi wmcStorageFeignApi;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "accEmployeeGroup", value = "该数据记录的POJO") @RequestBody AccEmployeeGroup accEmployeeGroup) {
        accEmployeeGroupService.save(accEmployeeGroup);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        accEmployeeGroupService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "accEmployeeGroup", value = "需要修改的内容") @RequestBody AccEmployeeGroup accEmployeeGroup) {
        accEmployeeGroup.setUpdateTime(new Date());
        accEmployeeGroupService.update(accEmployeeGroup);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<AccEmployeeGroup> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        AccEmployeeGroup accEmployeeGroup = accEmployeeGroupService.findById(id);
        return WrapMapper.ok(accEmployeeGroup);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<AccEmployeeGroup>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        System.out.println("分页获取所有记录"+page);
        AccEmployeeInfoDto accEmployeeInfoDto = new AccEmployeeInfoDto();
        PageHelper.startPage(page, size);
        List<AccEmployeeInfoDto> list = accEmployeeGroupService.selectEmployInfo(accEmployeeInfoDto);
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    @GetMapping("/getEmployeeInfo")
    @ApiOperation(httpMethod = "GET", value = "获取所有记录")
    public Wrapper<List<AccEmployeeInfoDto>> detailList(@ApiParam(name = "userName", value = "名称", defaultValue = "0") @RequestParam(required = false) String userName,
                                                        @ApiParam(name = "employeeType", value = "类型", defaultValue = "0") @RequestParam(required = false) Byte employeeType,
                                                        @ApiParam(name = "employeeStatus", value = "状态", defaultValue = "0") @RequestParam(required = false) Byte employeeStatus,
                                                        @ApiParam(name = "employeeDriver", value = "是否司机", defaultValue = "0") @RequestParam(required = false) Byte employeeDriver

                                                        ) {
        AccEmployeeInfoDto accEmployeeInfoDto = new AccEmployeeInfoDto();
        accEmployeeInfoDto.setUserName(userName);
        accEmployeeInfoDto.setEmployeeType(employeeType);
        accEmployeeInfoDto.setEmployeeStatus(employeeStatus);
        accEmployeeInfoDto.setEmployeeDriver(employeeDriver);

        List<AccEmployeeInfoDto> list = accEmployeeGroupService.selectEmployInfo(accEmployeeInfoDto);
        return WrapMapper.ok(list);
    }

    /**
     * 司机上线打卡、请假
     * user_id,longitude,latitude,employeeStatus
     */
    @PostMapping("/clockIn")
    @ApiOperation(httpMethod = "POST", value = "司机完成打卡、请假动作")
    public Wrapper clockIn(@RequestBody JSONObject jsonObject){
        System.out.println(jsonObject);
        //1.判断参数是否正常
        if (jsonObject == null || jsonObject.get("userId") == null){
            return WrapMapper.error("参数错误！用户ID未收到！");
        }
        if (jsonObject.get("longitude") == null || jsonObject.get("longitude") == "" || jsonObject.get("latitude") == ""){
            logger.error("参数错误！用户定位信息未收到!");
            return WrapMapper.error("参数错误！用户定位信息未收到！");
        }
        Long userId = Long.parseLong(jsonObject.get("userId").toString());
        Byte employeeStatus = Byte.valueOf(jsonObject.get("employeeStatus").toString());
        UserGroupDto userGroupDto = accEmployeeGroupService.selectEmployeeGroupInfoByUserId(userId);

        if (userGroupDto.getEmployeeStatus().equals(employeeStatus) && userGroupDto.getEmployeeStatus().equals((byte)1)){
            return WrapMapper.error("您已经开工状态！");
        }

        if (userGroupDto.getEmployeeStatus().equals(employeeStatus) && userGroupDto.getEmployeeStatus().equals((byte)2)){
            return WrapMapper.error("您已经在请假状态！");
        }

        //2.判断位置参数是否在范围内
        Double longitude = Double.valueOf(jsonObject.get("longitude").toString());
        Double latitude = Double.valueOf(jsonObject.get("latitude").toString());
        if (employeeStatus.equals((byte)1)){ // 只有开工打卡时才检查距离
            List<Map> warehouseInfo = wmcStorageFeignApi.findByPostion(latitude,longitude,100L);

            if (warehouseInfo == null && warehouseInfo.size() < 1){
                return WrapMapper.error("您不在打卡范围内");
            }
            //userGroupDto.setWarehouseId(Long.parseLong(warehouseInfo.get(0).get("warehouseId").toString()));
            //userGroupDto.setWarehouseName(warehouseInfo.get(0).get("warehouseName").toString());
        }


        //3.完成打卡/请假动作
        AccEmployeeGroup accEmployeeGroup = new AccEmployeeGroup();
        accEmployeeGroup.setEmployeeGroupId(userGroupDto.getEmployeeGroupId());
        accEmployeeGroup.setUserId(userId);
        accEmployeeGroup.setEmployeeStatus(employeeStatus);
        accEmployeeGroup.setUpdateTime(new Date());
        accEmployeeGroupService.update(accEmployeeGroup);

        //4.完成打卡日志
        AccCheckinLog accCheckinLog = new AccCheckinLog();
        accCheckinLog.setUserId(userId);
        accCheckinLog.setUserStatus(employeeStatus);
        accCheckinLog.setUserType(userGroupDto.getEmployeeType());
        accCheckinLog.setCreateTime(new Date());
        accCheckinLogService.save(accCheckinLog);
        return WrapMapper.ok();
    }
}
